package com.gxa.zhonglong.controller;

import com.gxa.zhonglong.common.JsonResult;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.pojo.GoodsCondition;
import com.gxa.zhonglong.service.GoodsConditionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : 宸濯
 * Date: 2021/11/01 12:29
 * Description:
 * Version: V1.0
 */
@Api(tags = "商品成色api")
@CrossOrigin
@RestController
@RequestMapping("/goodsCondition")
public class GoodsConditionController {

    @Autowired
    private GoodsConditionService goodsConditionService;

    @PostMapping("/selectAll")
    @ApiOperation("查询所有商品成色")
    public Map<String,Object> selectAll(){
        List<Map<String, Object>> list = goodsConditionService.selectAll();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(list);
        return jsonResult.getDataMap();
    }

    @PostMapping("/addOrUpdate")
    @ApiOperation("添加/修改商品成色")
    public Map<String,Object> addOrUpdate(GoodsCondition goodsCondition) throws BusinessException {
        JsonResult jsonResult = new JsonResult();
        if (goodsCondition.getId()==null) {
            goodsConditionService.add(goodsCondition);
            jsonResult.setMsg("添加成功!");
        }else {
            goodsConditionService.update(goodsCondition);
            jsonResult.setMsg("修改成功!");
        }
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @PostMapping("/del")
    @ApiOperation("删除/多删除商品成色(单删除也传存id的数组)")
    public Map<String,Object> del(@RequestBody Map<String,List<Long>> ids) throws BusinessException {
        goodsConditionService.del(ids.get("ids"));
        final JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("删除成功!");
        return jsonResult.getDataMap();
    }
}
